﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BestLifePOS_Core.Data.Commands;
using BestLifePOS_Core.DomainObjects;
using BestLifePOS_Core.Data.Fascade;
using System.Linq;

namespace BestLifePOS_PresentationLayer
{
    public partial class frmNewPR : Form
    {
        string GVtext = "text";
        double GvTotal = 0.00;
        DataTable LvTable3 = new DataTable("Details");
        public frmNewPR()
        {
            InitializeComponent();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            txtPRId.Text = Purchase_RequestDAO.GetNewPRId().ToString();
        }

        private void frmNewPR_Load(object sender, EventArgs e)
        {
            FillBranchDropDown();
            FillProductDropDown();
            LoadPriceListIds(); 
            FillGrid3();
        }

        private void FillBranchDropDown()
        {
            //Fill the DataGrid
            List<Branch> LvList = BranchDAO.GetAllCompanyBranches();
            DataTable LvTable = new DataTable();
            //dgvGRN.DataSource = LvTable3;

            //cmbCategory.DataSource = LvTable;
            LvTable.Columns.Add("BranchID");
            LvTable.Columns.Add("BranchName");

            string lvtest = "";

            foreach (Branch LvBranch in LvList)
            {
                //LvTable = (DataTable)cmbCategory.DataSource;

                LvTable.Rows.Add(LvTable.NewRow());

                lvtest = LvBranch.Branch_Id.ToString() + '-' + LvBranch.Branch_Name.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][0] = lvtest;
                //LvTable.Rows[LvTable.Rows.Count - 1][0] = LvCategory.Cat_ID.ToString() +'-' + LvCategory.Cat_Name.ToString();
                //LvTable.Rows[LvTable.Rows.Count - 1][1] = LvSubCategory.Subcat_ID.ToString();


            }

            cmbBranchId.DisplayMember = "BranchID";
            cmbBranchId.ValueMember = "BranchID";
            cmbBranchId.DataSource = LvTable;


            //cmbCategory.DataBindings();



        }


        private void FillProductDropDown()
        {
            //Fill the DataGrid
            List<Product> LvList = ProductDAO.GetAllProduct();
            DataTable LvTable = new DataTable();
            //dgvGRN.DataSource = LvTable3;

            //cmbCategory.DataSource = LvTable;
            LvTable.Columns.Add("ProductID");
            LvTable.Columns.Add("ProductName");

            string lvtest = "";

            foreach (Product LvProduct in LvList)
            {
                //LvTable = (DataTable)cmbCategory.DataSource;

                LvTable.Rows.Add(LvTable.NewRow());

                lvtest = LvProduct.Pro_ID.ToString() + '-' + LvProduct.Pro_Name.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][0] = lvtest;
                //LvTable.Rows[LvTable.Rows.Count - 1][0] = LvCategory.Cat_ID.ToString() +'-' + LvCategory.Cat_Name.ToString();
                //LvTable.Rows[LvTable.Rows.Count - 1][1] = LvSubCategory.Subcat_ID.ToString();


            }

            cmbProduct.DisplayMember = "ProductID";
            cmbProduct.ValueMember = "ProductID";
            cmbProduct.DataSource = LvTable;
            


            //cmbCategory.DataBindings();



        }

        private void cmbProduct_SelectedIndexChanged(object sender, EventArgs e)
        {
            lblProductName.Text = cmbProduct.Text.ToString().Substring(8, cmbProduct.Text.ToString().Length - 8);
            LoadPriceListIds();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            LvTable3.Rows.Add(LvTable3.NewRow());
            LvTable3.Rows[LvTable3.Rows.Count - 1][0] = cmbProduct.Text.ToString().Substring(0, 7);
            LvTable3.Rows[LvTable3.Rows.Count - 1][1] = lblProductName.Text.ToString();
            LvTable3.Rows[LvTable3.Rows.Count - 1][2] = cmbPriceList.Text.ToString().Substring(0,7);
            LvTable3.Rows[LvTable3.Rows.Count - 1][3] = cmbPriceList.Text.ToString().Substring(8, cmbPriceList.Text.ToString().Length-8);
            LvTable3.Rows[LvTable3.Rows.Count - 1][4] = txtQty.Text.ToString();



            dgvItemList.DataSource = LvTable3;

            txtQty.Text = "";

        }

        private void FillGrid3()
        {

            dgvItemList.DataSource = LvTable3;
            LvTable3.Columns.Add("ProductId ");
            LvTable3.Columns.Add("ProductName ");
            LvTable3.Columns.Add("PriceListId ");
            LvTable3.Columns.Add("UnitPrice ");
            LvTable3.Columns.Add("Qty ");

        }

        private void LoadPriceListIds()
        {

            List<Price_List> LvList = Price_ListDAO.GetPrice_ListbyItemID(cmbProduct.Text.ToString().Substring(0,7));

            DataTable LvTable = new DataTable();

            LvTable.Columns.Add("PriceListId ");


            foreach (Price_List LvPRLId in LvList)
            {
                LvTable.Rows.Add(LvTable.NewRow());

                LvTable.Rows[LvTable.Rows.Count - 1][0] = LvPRLId.PL_Id + "-" + LvPRLId.Pro_Price;
            }

            cmbPriceList.DisplayMember = "PriceListId ";
            cmbPriceList.DataSource = LvTable;

        }

        private void btnSave_Click(object sender, EventArgs e)
        {

            DataSet ds = new DataSet("prdetails");

            string detailsXML = "";

            ds.Tables.Add(LvTable3.Copy());
            detailsXML = ds.GetXml();

            SavePR(detailsXML);
            MessageBox.Show("PR Was Succesfuly Saved");


        }

        private int SavePR(string xmlstring)
        {
            string Lvxml = xmlstring;

            Purchase_Request LvPR = CollectPRFigures();
            if (LvPR != null)
            {
                return Purchase_RequestDAO.SavePR(LvPR, Lvxml);
            }
            else
            {
                return 0;
            }
        }

        private Purchase_Request CollectPRFigures()
        {

            try
            {

                //getting the sumof qty

                for(int i =0; i< dgvItemList.RowCount-1; i++)
                {
                    double amount = double.Parse(dgvItemList.Rows[i].Cells[4].Value.ToString()) *double.Parse(dgvItemList.Rows[i].Cells[3].Value.ToString()) ;
                    GvTotal +=amount;

                }

                Purchase_Request LvPRMaster = new Purchase_Request();

                LvPRMaster.Pr_Id = txtPRId.Text.ToString();
                LvPRMaster.Branch_Id = cmbBranchId.Text.ToString().Substring(0,cmbBranchId.Text.IndexOf('-'));
                LvPRMaster.Accept_Date = dtpRequired.Value;
                LvPRMaster.Description = rtxtDesc.Text.ToString();
                LvPRMaster.Total = Decimal.Parse(GvTotal.ToString());
                LvPRMaster.User_Id = "1";
                LvPRMaster.Create_Date = DateTime.Now;


                return LvPRMaster;
            }
            catch (Exception)
            {
                return null;
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            frmReportViewer frmR = new frmReportViewer(1, txtPRId.Text.ToString());
            frmR.ShowDialog();
        }
    }
}
